package com.happyface.spanablestring;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = (TextView) findViewById(R.id.text);
        String content = "根据当前Item的位置确定绘制分组的位置。top将在mGroupHeight和view.getTop()中取最小值";
        String keyword = "绘制";
        int index = content.indexOf(keyword);
        int length = keyword.length();
        ClickableSpan span1 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "ahahaha", Toast.LENGTH_LONG).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);//是否显示下划线
                ds.setColor(Color.parseColor("#5897ce"));
            }
        };
        ClickableSpan span2 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(MainActivity.this, "ahahaha", Toast.LENGTH_LONG).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);//是否显示下划线
                ds.setColor(Color.parseColor("#5897ce"));
            }
        };
        SpannableStringBuilder builder = new SpannableStringBuilder(content);
        ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
        builder.setSpan(span, index, index + length, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        builder.setSpan(span1, 0, 4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        builder.setSpan(span2, 10, 15, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        textView.setText(builder);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
//        String user1 = "我是一只小小鸟";
//        String user2 = "他是一只大笨猪";
//        String text = user1 + "回复" + user2 + "：你是傻逼吗？哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈";
//        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED);
//        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(Color.parseColor("#9e9e9e"));
//        ClickableSpan clickableSpan = new ClickableSpan() {
//            @Override
//            public void onClick(View widget) {
//                Toast.makeText(MainActivity.this, "haha", Toast.LENGTH_LONG).show();
//            }
//
//            @Override
//            public void updateDrawState(TextPaint ds) {
//                super.updateDrawState(ds);
//                ds.setUnderlineText(false);//是否显示下划线
//                ds.setColor(getResources().getColor(R.color.colorPrimary));
//            }
//
//        };
//        ClickableSpan clickableSpan1 = new ClickableSpan() {
//            @Override
//            public void onClick(View widget) {
//                Toast.makeText(MainActivity.this, "haha", Toast.LENGTH_LONG).show();
//            }
//
//            @Override
//            public void updateDrawState(TextPaint ds) {
//                super.updateDrawState(ds);
//                ds.setUnderlineText(false);//是否显示下划线
//                ds.setColor(getResources().getColor(R.color.colorPrimary));
//            }
//
//        };
//        /**
//         * 带点击事件的文本部分，一处对应一个clickableSpan,点击事件的text颜色updateDrawState方法里设置
//         */
//        SpannableStringBuilder builder = new SpannableStringBuilder(text);
//        int length1 = user1.length();
//        int length2 = user2.length();
////        builder.setSpan(colorSpan,0,length1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//        builder.setSpan(colorSpan1, length1, length1 + 2, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
////        builder.setSpan(colorSpan,length1+2,length1+length2+2,Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//        builder.setSpan(colorSpan1, length1 + length2 + 2, builder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//        builder.setSpan(clickableSpan1, 0, length1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//        builder.setSpan(clickableSpan, length1 + 2, length1 + length2 + 2, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//        textView.setText(builder);
//        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}
